home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK3HW1.TXT < prev    next >
Text File  |  1996-06-06  |  9KB  |  267 lines

  1. Week 3 Homework 1
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6. In our first Week 3 installment (WK3CM1.TXT) we covered the OUTPUT and INPUT
  7. file modes.  These are for writing and reading files, respectively.  This
  8. part of the lesson covers a new file mode called APPEND.  We will also learn
  9. how to use the LINE INPUT statement, and we will also use a few other new
  10. Liberty BASIC statements in passing.
  11.  
  12. The APPEND file mode
  13. =============================================================================
  14.  
  15. There is a limitation in the OUTPUT file mode.  The OUTPUT mode is only good
  16. for creating new files because it always deletes the contents of any file
  17. opened when it is used.  For example:
  18.  
  19.     'open a file and print some information into it
  20.     open "numbers.txt" for output as #out
  21.     for x = 1 to 10
  22.         print #out, x
  23.     next x
  24.     close #out
  25.  
  26.     'now rewrite the file, replacing its original contents
  27.     open "numbers.txt" for output as #out
  28.     for x = 11 to 20
  29.         print #out, x
  30.     next x
  31.  
  32.     end
  33.  
  34. Run the above program and open Notepad on NUMBERS.TXT.  You'll see that only
  35. the numbers 11 to 20 will be in the file.  The only way to retain the
  36. original information in the file and add new information using the OUTPUT
  37. mode would be to read the original information and insert it into a new
  38. file.  Then the new information is inserted into the still open file.
  39. Here is what that code looks like:
  40.  
  41.     'open a file, and print some information into it, then close it
  42.     open "numbers.txt" for output as #out
  43.     for x = 1 to 10
  44.         print #out, x
  45.     next x
  46.     close #out
  47.  
  48.     'open the original file for input
  49.     open "numbers.txt" for input as #in
  50.  
  51.     'open the new file for output
  52.     open "numbers2.txt" for output as #out
  53.  
  54. [copyLoop]  'copy the contents of numbers.txt to numbers2.txt
  55.     if eof(#in) = -1 then [doneCopying]
  56.     input #in, value
  57.     print #out, value
  58.     goto [copyLoop]
  59.  
  60. [doneCopying]  'close numbers.txt & print new numbers into numbers2.txt
  61.     close #in
  62.     for x = 11 to 20
  63.         print #out, x
  64.     next x
  65.     close #out
  66.  
  67.     'delete the original numbers.txt file
  68.     kill "numbers.txt"
  69.     'rename numbers2.txt to numbers.txt
  70.     name "numbers2.txt" as "numbers.txt"
  71.  
  72.     end
  73.  
  74. Once the new file has been created containing the contents of the original
  75. file, plus our new numbers 11 to 20, we delete the old file using the KILL
  76. statement and rename the new file using the NAME statement.
  77.  
  78. There is a better way.  Using the APPEND file mode, we can eliminate a lot
  79. of program code.  The APPEND mode opens a file for writing, just like the
  80. OUTPUT mode does.  Instead of erasing the contents of the file opened and
  81. starting at the beginning, the APPEND mode opens a file and retains
  82. everything.  All we need to do then is write to the file, and everything we
  83. write will be added to the end of the file.  Here is the example above
  84. rewritten using the APPEND mode:
  85.  
  86.     'open a file, and print some information into it, then close it
  87.     open "numbers.txt" for output as #out
  88.     for x = 1 to 10
  89.         print #out, x
  90.     next x
  91.     close #out
  92.  
  93.     'open the original file for append
  94.     open "numbers.txt" for append as #out
  95.     for x = 11 to 20
  96.         print #out, x
  97.     next x
  98.     close #out
  99.  
  100.     end
  101.  
  102. This is much shorter and simpler than the last example.  When we are done
  103. writing the numbers 11 to 20 there is no need to delete or rename files.
  104.  
  105.  
  106. Comma delimited files
  107. ---------------------------------------------------------------------------
  108.  
  109. By default, the standard way of reading from a sequential file using the
  110. INPUT statement breaks up the data we read at each end of line, and at each
  111. comma.  Look at these equivalent file contents:
  112.  
  113. File 1 contents:
  114.  
  115. 1,2,3,4,5,6,7,8,9,10
  116.  
  117. File 2 contents:
  118.  
  119. 1,2,3,4,5
  120. 6,7,8,9,10
  121.  
  122. File 3 contents:
  123.  
  124. 1
  125. 2
  126. 3
  127. 4
  128. 5
  129. 6
  130. 7
  131. 8
  132. 9
  133. 10
  134.  
  135. All three of the above possible file contents would produce the same
  136. displayed output using a loop like the following:
  137.  
  138. [loop]  'display each item on its own line
  139.     if eof(#in) = -1 then [stopLooping]
  140.     input #in, item$  'read the next item
  141.     print item$
  142.     goto [loop]
  143.  
  144. Since the comma is used to separate items (in addition to the end of line),
  145. this kind of file format is often called 'comma delimited'.  This is a
  146. preferred generic format often used by commercial application software
  147. (spreadsheets are a good example) as a data export option.  This is so that
  148. people like you and me can write our own customized software to use that
  149. information.
  150.  
  151. What if we want to use commas in our information?  As an example, we decide
  152. that we will build an application that logs responses to a direct mail
  153. campaign, and we want the name to be entered in a single field.  The name
  154. will be entered last name first, with a comma separating the last from the
  155. first name (example: Doe, John).
  156.  
  157. Take a look at this hypothetical file entry:
  158.  
  159. Doe, John
  160. 123 Main Street
  161. Scottsdale
  162. AZ
  163. 01234
  164.  
  165. Now look at this code fragment:
  166.  
  167.     input #in, name$
  168.     input #in, street$
  169.     input #in, city$
  170.     input #in, state$
  171.     input #in, zip$
  172.  
  173. If we read the file entry with the above code, name$ would only contain up to
  174. the first comma.  So name$ would contain the string "Doe", street$ would
  175. contain "John", city$ would contain "123 Main Street", and so on.  This is
  176. not what we want.
  177.  
  178. There is a special form of the INPUT statement that will read a whole line
  179. from a file, including commas.  It is the LINE INPUT statement.  Here is the
  180. code fragment above rewritten using LINE INPUT:
  181.  
  182.     line input #in, name$
  183.     line input #in, street$
  184.     line input #in, city$
  185.     line input #in, state$
  186.     line input #in, zip$
  187.  
  188. Another example of where LINE INPUT is useful is reading of a text document,
  189. like this one.  There are many commas in this document.  Here is a short
  190. program that uses the FILEDIALOG statement to ask for a *.txt file to read.
  191. Then it opens the file and reads and displays it.
  192.  
  193.     'ask for a filename
  194.     filedialog "Select a text file", "*.TXT", filename$
  195.     'if filename$ = "" then the user canceled the file selection
  196.     if filename$ = "" then end
  197.  
  198.     'open the file
  199.     open filename$ for input as #in
  200.  
  201. [readLoop]  'read and display each line in the text file
  202.     if eof(#in) = -1 then [stopLooping]
  203.     line input #in, lineOfText$
  204.     print lineOfText$
  205.     goto [readLoop]
  206.  
  207. [stopLooping]  'close the file
  208.     close #in
  209.  
  210.     end
  211.  
  212. Run this small program and try it with a file that you know has some commas
  213. in it.  Then change the line containing the LINE INPUT statement to use a
  214. regular INPUT statement and run the program again on the same file so you can
  215. see how they function differently.
  216.  
  217.  
  218. Here's the assignment
  219. -------------------------------------------------------------------------
  220.  
  221. Create a program called CALLER.BAS.  This program will give the user
  222. the following options:
  223.  
  224.   1) Enter a phone call
  225.   2) Search by caller's name
  226.   3) Search by person called
  227.   3) Quit
  228.  
  229. Menu item 1 will ask the user for phone call information and append it as
  230. a new record to the end of a file named PHONELOG.TXT.  The added record must
  231. include:
  232.  
  233.   1) Caller's name
  234.   2) Name of person called
  235.   3) Date of the call
  236.   4) Time of the call
  237.   5) A one line description of the call's purpose
  238.   6) A phone # where the caller can be reached
  239.  
  240. When searching, the program should stop and display the six data items for
  241. each record that matches and ask if the user wants to keep searching or
  242. quit the search.  The program will display a notice if no matches are found.
  243.  
  244.  
  245. Possible enhancement to CALLER.BAS
  246. -------------------------------------------------------------------------
  247.  
  248. It is possible to search for a partial match.  Look at the following code
  249. example:
  250.  
  251.     text$ = "The quick brown fox jumped over the lazy dog."
  252.     searchFor$ = "FOX"  'change FOX to different values and re-run
  253.     print "Searching for:"
  254.     print "  "; searchFor$
  255.     print "In the following:"
  256.     print "  "; text$
  257.     a$ = upper$(text$)
  258.     b$ = upper$(searchFor$)
  259.     if instr(a$, b$) > 0 then print "Found!" else print "Not Found!"
  260.  
  261. The UPPER$() function eliminates any letter case differences.  The INSTR()
  262. function returns a 0 value if our uppercased searchFor$ is not found in
  263. our uppercased text, or it returns the position of searchFor$ if it is
  264. found.
  265.  
  266.  
  267.